﻿using System;
using System.Data;
using System.Data.SqlClient;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Microsoft.Practices.EnterpriseLibrary.Data.Sql;
using Csla;

namespace Model
{
    [Serializable()]
    public class GiaDatBOCollection : Csla.BusinessListBase<GiaDatBOCollection, GiaDatBO>
    {
        #region BindingList Overrides
        protected override object AddNewCore()
        {
            GiaDatBO _GiaDatBO = GiaDatBO.NewChildGiaDatBO();
            OnAddingNew(new System.ComponentModel.AddingNewEventArgs(_GiaDatBO));
            Add(_GiaDatBO);
            return _GiaDatBO;
        }
        #endregion BindingList Overrides
        //==========================================================================

        #region Factory Methods
        private GiaDatBOCollection()
        {
        }

        public static GiaDatBOCollection NewGiaDatBOCollection()
        {
            return new GiaDatBOCollection();
        }

        public static GiaDatBO NewGiaDatBO()
        {
            return GiaDatBO.NewChildGiaDatBO();
        }

        public static GiaDatBOCollection GetGiaDatBOCollection()
        {
            SPParam spParam = new SPParam("GiaDatID", "ASC", "WHERE DELE=0");
            return DataPortal.Fetch<GiaDatBOCollection>(spParam);
        }

        public static GiaDatBOCollection GetGiaDatBOCollection(bool getDeleted)
        {
            SPParam spParam = new SPParam("GiaDatID", "ASC", "");
            return DataPortal.Fetch<GiaDatBOCollection>(spParam);
        }

        public static GiaDatBOCollection GetGiaDatBOCollection(string where)
        {
            SPParam spParam = new SPParam("GiaDatID", "ASC", "WHERE DELE=0" + (where != "" ? " AND " : "") + where);
            return DataPortal.Fetch<GiaDatBOCollection>(spParam);
        }

        public static GiaDatBOCollection GetGiaDatBOCollection(string where, bool getDeleted)
        {
            SPParam spParam = new SPParam("GiaDatID", "ASC", (where != "" ? "WHERE " : "") + where);
            return DataPortal.Fetch<GiaDatBOCollection>(spParam);
        }

        public static GiaDatBOCollection GetGiaDatBOCollection(string where, string orderByColumn)
        {
            SPParam spParam = new SPParam((orderByColumn == "" ? "GiaDatID" : orderByColumn), "ASC", "WHERE DELE=0" + (where != "" ? " AND " : "") + where);
            return DataPortal.Fetch<GiaDatBOCollection>(spParam);
        }

        public static GiaDatBOCollection GetGiaDatBOCollection(string where, string orderByColumn, bool getDeleted)
        {
            SPParam spParam = new SPParam((orderByColumn == "" ? "GiaDatID" : orderByColumn), "ASC", (where != "" ? "WHERE " : "") + where);
            return DataPortal.Fetch<GiaDatBOCollection>(spParam);
        }

        public static GiaDatBOCollection GetGiaDatBOCollection(string where, string orderByColumn, string orderByExpression)
        {
            SPParam spParam = new SPParam((orderByColumn == "" ? "GiaDatID" : orderByColumn), (orderByExpression == "" ? "ASC" : orderByExpression), "WHERE DELE=0" + (where != "" ? " AND " : "") + where);
            return DataPortal.Fetch<GiaDatBOCollection>(spParam);
        }

        public static GiaDatBOCollection GetGiaDatBOCollection(string where, string orderByColumn, string orderByExpression, bool getDeleted)
        {
            SPParam spParam = new SPParam((orderByColumn == "" ? "GiaDatID" : orderByColumn), (orderByExpression == "" ? "ASC" : orderByExpression), (where != "" ? "WHERE " : "") + where);
            return DataPortal.Fetch<GiaDatBOCollection>(spParam);
        }
        #endregion Factory Methods
        //==========================================================================

        #region Criteria
        [Serializable()]
        private class Criteria
        {
            public Criteria()
            {
            }
        }

        [Serializable()]
        public class FilterCriteria
        {
            public FilterCriteria()
            {
            }
        }
        #endregion Criteria
        //==========================================================================

        #region Fetch
        [Transactional(TransactionalTypes.TransactionScope)]
        private void DataPortal_Fetch(SPParam spParam)
        {
            RaiseListChangedEvents = false;
            SqlDatabase db = new SqlDatabase(Session.DBConnectionString);
            SqlCommand cmd = (SqlCommand)db.GetStoredProcCommand("GiaDatBO_SelectBOCollection");
            db.AddInParameter(cmd, "@OrderColumn", SqlDbType.VarChar, (object)spParam.orderColumn);
            db.AddInParameter(cmd, "@OrderExpression", SqlDbType.VarChar, (object)spParam.orderExpression);
            db.AddInParameter(cmd, "@WhereExpression", SqlDbType.NVarChar, (object)spParam.whereExpression);
            using (SqlDataReader dr = (SqlDataReader)db.ExecuteReader(cmd))
            {
                int stt = 0;
                while (dr.Read())
                {
                    GiaDatBO objGiaDatBO = GiaDatBO.GetChildGiaDatBO(dr);
                    objGiaDatBO.STT0 = ++stt;
                    this.Add(objGiaDatBO);
                }
            }
            RaiseListChangedEvents = true;
        }
        #endregion Fetch
        //==========================================================================

        #region Update
        [Transactional(TransactionalTypes.TransactionScope)]
        protected override void DataPortal_Update()
        {
            Update();
        }

        internal void Update()
        {
            RaiseListChangedEvents = false;
            GiaDatBO child = null;
            int length = this.Count;
            int stt = 0;
            bool del = false;
            for (int i = 0; i < length; i++)
            {
                child = this[i];
                del = StaticUtil.GetBool(child.CL_DELE);

                if (child.IsNew && !del)
                    child.Insert();
                else
                    child.Update();

                if (del)
                {
                    this.RemoveAt(i);
                    i--;
                    length--;
                }
                else
                {
                    child.STT0 = ++stt;
                }
            }
            RaiseListChangedEvents = true;
        }
        #endregion Update
        //==========================================================================

        #region Business Method
        public GiaDatBO GetGiaDatBO(int _GiaDatID)
        {
            GiaDatBO returnObject = null;
            foreach (GiaDatBO _GiaDatBO in this)
            {
                if (_GiaDatBO.CL_GiaDatID == _GiaDatID)
                {
                    returnObject = _GiaDatBO;
                    break;
                }
            }
            return returnObject;
        }

        public GiaDatBO GetGiaDatBO(int? TenDatID, int? NhomGiaDatID, int? ViTri)
        {
            if (TenDatID == null || NhomGiaDatID == null || ViTri == null)
                return null;

            int length = this.Count;
            for (int i = 0; i < length; i++)
                if (this[i].CL_NhomGiaDatID == NhomGiaDatID)
                    if (this[i].CL_TenDatID == TenDatID)
                        if (this[i].CL_ViTri == ViTri)
                            return this[i];

            return null;
        }

        public void Remove(int GiaDatID)
        {
            foreach (GiaDatBO _GiaDatBO in this)
            {
                if (_GiaDatBO.CL_GiaDatID == GiaDatID)
                {
                    try
                    {
                        this.Remove(_GiaDatBO);
                    }
                    catch { }
                    break;
                }
            }
        }

        public ViTriBOList FilterViTri(int? TenDatID, int? NhomGiaDatID)
        {
            ViTriBOList viTriBOList = ViTriBOList.NewViTriBOList();

            int length = this.Count;
            for (int i = 0; i < length; i++)
                if (this[i].CL_TenDatID == TenDatID && this[i].CL_NhomGiaDatID == NhomGiaDatID)
                    if (StaticUtil.GetInt(this[i].CL_ViTri, -1) != -1)
                        viTriBOList.AddCustomValue(this[i].CL_ViTri.Value, this[i].CL_ViTri.Value.ToString());

            return viTriBOList;
        }
        #endregion Business Method
        //==========================================================================
    }
}
